<template>
    <view class="content">
        <y-video-slide
            ref="videoList"
            :video-height="screenHeight + 'px'"
            :isHide="isHide"
            :data="videoData"
            @refresh="refresh"
            @loadMore="loadMore"
            @share="share"
            @goToGoodsFunc="goToGoodsFunc"
            @goToBiaoQian="goToBiaoQian"
            @fabulous="fabulous"
            @follow="follow"
            @commentFabulous="commentFabulous"
        ></y-video-slide>
    </view>
</template>

<script>
import yVideoSlide from "../components/y-video-slide/y-video-slide.vue";
import { baseUrl } from "@/config/tendIdConfig";

// 业务员和客户 目前都用客户的token 统一拦截那边已经做过处理 所以视频这里单拉出来 没走拦截器
const token =
    uni.getStorageSync("customer_token") || uni.getStorageSync("access_token");

export default {
    data() {
        return {
            clickId: "",
            pageNum: 1,
            pageSize: 20,
            isHide: false,
            screenHeight: uni.getSystemInfoSync().windowHeight,
            videoData: [],
            currentVideoData: {},
        };
    },
    components: {
        yVideoSlide,
    },
    onLoad(option) {
        this.clickId = option.clickId;
    },
    onReady() {
        this.pageNum = 0;
        this.getVideoList();
    },
    onUnload() {
        this.$refs.videoList.stopOtherVideo();
    },
    onHide() {
        this.isHide = true;
        this.$refs.videoList.stopOtherVideo();
    },
    onShow() {
        this.isHide = false;
    },
    onShareAppMessage(res) {
        return {
            title:
                this.currentVideoData?.videoName ??
                this.currentVideoData?.videoIntro,
            imageUrl: this.currentVideoData?.videoCover ?? "",
            path:
                "pages_video/videoNew/index?clickId=" +
                this.currentVideoData?.id,
        };
    },
    onShareTimeline() {
        return {
            title:
                this.currentVideoData?.videoName ??
                this.currentVideoData?.videoIntro,
            imageUrl: this.currentVideoData?.videoCover ?? "",
            path:
                "pages_video/videoNew/index?clickId=" +
                this.currentVideoData?.id,
        };
    },
    methods: {
        refresh() {
            this.pageNum = 0;
            this.getVideoList();
        },
        loadMore() {
            this.getVideoList();
        },
        share(video) {
            this.currentVideoData = video;
            console.log(video);
        },
        goToGoodsFunc(video) {
            uni.$u.route({
                url: `pages_shop1/goods/index?id=${video.goodsId}`,
            });
        },
        // 标签
        goToBiaoQian(tag) {
            this.$u.route({
                url: "/pages_video/video/index",
                params: {
                    styleFirstNum: 1,
                    tag,
                },
            });
        },
        fabulous(video, flag) {
            console.log("flag", video, flag);
            uni.request({
                url: `${baseUrl}/customer/video_info_customer/open/giveALike/${
                    video.id
                }/${flag ? 1 : 0}`,
                method: "GET",
                header: {
                    Authorization: token,
                },
                success: (res) => {
                    console.log("object :>> ", res);
                },
            });
        },
        follow(video, flag) {
            console.log(video, flag);
        },
        commentFabulous(comment) {
            console.log(comment);
        },
        getVideoList() {
            const that = this;
            this.pageNum += 1;
            uni.request({
                url:
                    baseUrl +
                    "/customer/video_info_customer/open/pageVideoNoToken",
                method: "POST",
                header: {
                    Authorization: token,
                },
                data: {
                    pageNum: this.pageNum,
                    pageSize: this.pageSize,
                    clickId: this.clickId,
                    recordTypeSet: [0, 1, 8],
                    // tenantId: this.$tendId,
                },
                success(res) {
                    console.log(res);
                    const result = res.data;
                    if (!result.data.rows || result.data.rows.length === 0) {
                        that.pageNum -= 1;
                    }
                    const dataList = result.data.rows.map((item) => {
                        return {
                            postUrl: item.videoCover,
                            userHead: item.videoCover,
                            fabulousCount: item.likeNum,
                            isFabulous: item.likeStatus,
                            userNick: item.videoName,
                            videoContent: item.videoIntro,
                            ...item,
                        };
                    });
                    that.videoData = that.videoData.concat(dataList);
                    that.currentVideoData = that.videoData[0];
                    console.log(
                        dataList,
                        "111",
                        that.videoData,
                        that.currentVideoData
                    );
                },
            });
        },
    },
};
</script>

<style lang="scss">
.content {
    height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background-color: #000;
}
</style>
